﻿using FoodModel;
using J.FoodManageGet.Application.Command.SpecificationCommand;
using J.FoodRepository.InterFace;
using MediatR;
using Microsoft.IdentityModel.Tokens;

namespace J.FoodManageGet.Application.CommandHandler.SpecificationCommandHandler
{
    /// <summary>
    /// 菜品规格反填
    /// </summary>
    public class SpecificationBackCommandHandler : IRequestHandler<SpecificationBackCommand, List<SpecificationModel>>
    {
        private readonly ISpecificationRepository _specificationRepository;

        public SpecificationBackCommandHandler(ISpecificationRepository specificationRepository)
        {
            _specificationRepository = specificationRepository;
        }
        public async Task<List<SpecificationModel>> Handle(SpecificationBackCommand request, CancellationToken cancellationToken)
        {
            var list = await _specificationRepository.GetAsync(x =>
           (request.SpecificationId == 0 || x.SpecificationId == request.SpecificationId));
            return list.Select(specification =>
            {
                specification.Separates = specification.Separate == "" ? new List<string>() : specification.Separate.Split(',').ToList();
                return specification;
            }).ToList();
        }
    }
}
